/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vue;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import model.jeu_de_dame.Piece;

/**
 *
 * @author nabil
 */
public class PieceVue extends javax.swing.JLabel{
    protected Image image;
    
    public enum TYPE_PION
    {
        DAME_BLANC,DAME_NOIR,PION_BLANC,PION_NOIR
    }
    private static final EnumMap<TYPE_PION, String> tab;
    static{
        tab = new EnumMap<TYPE_PION, String>(TYPE_PION.class);
        tab.put(TYPE_PION.DAME_BLANC, "vue/resources/damebl.png");
        tab.put(TYPE_PION.DAME_NOIR, "vue/resources/damenoir.png");
        tab.put(TYPE_PION.PION_BLANC, "vue/resources/pionbl.png");
        tab.put(TYPE_PION.PION_NOIR, "vue/resources/pionnoir.png");
    }
    public PieceVue(Piece p,int taille, TYPE_PION t)
    {
        super();
        initComponents();
        this.setSize( taille, taille);
       ClassLoader classLoader = getClass().getClassLoader();
       URL file = classLoader.getResource(PieceVue.tab.get(t));
       ImageIcon im = new ImageIcon(file);
       image = im.getImage();
    }
   
    private void initComponents()
    {
       this.setVisible(true);
        
        addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pionMouseClicked(evt);
            }
        });
    }
    
    public void pionMouseClicked(MouseEvent event){
        try
        {
            CaseVue caseParent = (CaseVue)this.getParent();
            DamierView.getInstance().getControl().selectionPion(caseParent.getCoordonneX(), caseParent.getCoordonneY());
            System.out.println("apres select pion");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
     public void paint(Graphics g){
      g.drawImage(image,this.getWidth()/4, this.getHeight()/4, this.getWidth()/2, this.getHeight()/2, null, null);
    }
    
    public void update(Observable o, Object arg) {
        if(o instanceof Piece)
        {
            this.getParent().remove(this);
        }
    }
    
}
